package com.hmall.item.web;

import com.hmall.common.PageBean;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import com.hmall.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 查询商品列表 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageBean<Item> getItems(@RequestParam(value = "page") int page,
                                   @RequestParam(value = "size") int size) {
        List<Item> items = itemService.getItems(page, size);
        long total = itemService.getTotal();
        PageBean<Item> pageBean = new PageBean<>();
        pageBean.setList(items);
        pageBean.setTotal(total);
        return pageBean;
    }

    /**
     * 图片上传接口
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestBody MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            try {
                String result = aliOSSUtils.upload(file);
                log.info("图片上传成功 {}", result);
                return result;
            } catch (IOException e) {
                e.printStackTrace();
                return "图片上传失败";
            }
        }
        return "图片上传失败";
    }


    /**
     * 添加商品
     *
     * @param item
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addItem(@Validated @RequestBody Item item) {
        itemService.addItem(item);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateItem(@RequestBody Item item) {
        itemService.updateItem(item);
        return ResponseEntity.ok().build();
    }


    /**
     * 删除商品
     * 逻辑删除
     *
     * @param Id
     * @return
     */
    @DeleteMapping("/{Id}")
    public ResponseEntity<Void> remove(@PathVariable Long Id) {
        itemService.removeById(Id);
        return ResponseEntity.ok().build();
    }

    /**
     * 状态修改 上架 下架
     *
     * @param Id
     * @param status
     * @return
     */
    @PutMapping("/status/{Id}/{status}")
    public ResponseEntity<Void> changeStatus(@PathVariable Long Id, @PathVariable Long status) {
        itemService.updateStatus(Id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 处理根据id查询商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item getById(@PathVariable Long id) {
        return itemService.getById(id);
    }

    /**
     * 查询商品分类
     */
    @GetMapping("/getCategory")
    public List<Object> getCategory() {
        return itemService.getCategory();
    }

    /**
     * 查询品牌
     */
    @GetMapping("/getBrand")
    public List<Object> getBrand() {
        return itemService.getBrand();
    }


    /**
     * 处理库存修改
     *
     * @param itemId
     * @param num
     */
    @PutMapping("stock/{itemId}/{num}")
    public void updateStock(@PathVariable Long itemId, @PathVariable Integer num) {
        itemService.updateStock(itemId, num);
    }
}
